package com.guli.vod.service.impl;

import com.guli.base.exception.GuliException;
import com.guli.vod.service.VideoUploadService;
import com.guli.vod.utils.ConstantPropertiesUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author ZHAOZHENYU
 * @since 2022/9/29
 */

@Service
public class VideoUploadServiceImpl implements VideoUploadService {

    // 本地上传
    @Override
    public String uploadVideo(MultipartFile file) {
        String localBucketPath = ConstantPropertiesUtil.LOCAL_BUCKET_PATH;
        String nginxProxyUrl = ConstantPropertiesUtil.NGINX_PROXY_URL;
        try {
            String originalFilename = file.getOriginalFilename();
            String uuidStr = UUID.randomUUID().toString().replaceAll("-", "");
            String newFileName = uuidStr + originalFilename.substring(originalFilename.lastIndexOf("."));
            String localFile = localBucketPath + "/" + newFileName;
            file.transferTo(new File(localFile));
            return newFileName;
        } catch (IOException e) {
            e.printStackTrace();
            throw new GuliException(20001, "guli-vod上传失败");
        }
    }

    // 本地删除
    @Override
    public void removeVideo(String videoId) {
        String videoPath = ConstantPropertiesUtil.LOCAL_BUCKET_PATH + "/" + videoId;
        File file = new File(videoPath);
        boolean flag = file.delete();
        if (!flag) throw new GuliException(20001, "删除视频失败");
    }

    // 阿里云上传
    /*@Override
    public String uploadVideo(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            String uuidStr = UUID.randomUUID().toString().replaceAll("-", "");
            String title = uuidStr + originalFilename.substring(originalFilename.lastIndexOf("."));

            // 阿里云上传
            UploadStreamRequest request = new UploadStreamRequest(
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET,
                    title,originalFilename,inputStream
            );
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            String videoId = response.getVideoId();
            if (!response.isSuccess()) {
                String errorMessage = "上传错误：" + "code：" + response.getCode() + ", message：" + response.getMessage();
                if(StringUtils.isEmpty(videoId)){
                    throw new GuliException(20001, errorMessage);
                }
            }
            return videoId;
        } catch (IOException e) {
            e.printStackTrace();
            throw new GuliException(20001, "guli-vod上传失败");
        }
    }*/

    // 阿里云删除
   /* @Override
    public void removeVideo(String videoId) {
        try{
            DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai",
                    ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            DefaultAcsClient client = new DefaultAcsClient(profile);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoId);
            DeleteVideoResponse response = client.getAcsResponse(request);
            System.out.print("RequestId = " + response.getRequestId() + "\n");

        }catch (ClientException e){
            throw new GuliException(20001, "视频删除失败");
        }
    }*/

}
